package cn.operation.api.rest;

import cn.operation.api.dto.CvBrandDTO;
import cn.operation.api.dto.CvSeriesDTO;
import cn.operation.api.dto.ModelDTO;
import cn.operation.api.query.baseBrand.BrandQuery;
import cn.operation.api.query.baseBrand.SeriesQuery;
import cn.operation.backend.bo.baseBrand.ScBrandSeriesModelBo;
import cn.operation.backend.bo.baseBrand.SeriesQueryBo;
import cn.operation.backend.query.BrandQueryConditions;
import cn.operation.backend.query.SeriesQueryConditions;
import cn.operation.backend.service.baseBrand.ScBaseCvBrandService;
import cn.operation.backend.service.baseSeries.SeriesService;
import cn.operation.backend.util.BusinessException;
import cn.operation.backend.util.PageVo;
import cn.operation.backend.util.ResultDTOWithPagination;
import cn.operation.base.dto.extdto.baseBrand.ScBaseCvBrandDto;
import cn.operation.base.model.BrandSeriesModel;
import cn.operation.base.model.ScBaseCvBrand;
import cn.operation.base.model.ScBaseCvSeries;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;

/**
 * @FileName: com.unlcn.ils.sc.api.rest
 * @Description: 商品车品牌车系
 * @author: fancy
 * @date: 2017/5/24
 */
@Controller
@RequestMapping(value = "/api/brandSeries", produces = MediaType.APPLICATION_JSON_VALUE)
@Api(value = "/api/brandSeries", description = "商品车主数据，品牌、车型、车系")
public class BrandSeriesRest {

	private static final Logger LOGGER = LoggerFactory.getLogger(BrandSeriesRest.class);

	@Autowired
	private SeriesService seriesService;

	@Autowired
	private ScBaseCvBrandService scBaseCvBrandService;


	@RequestMapping(value = "/getBrands", method = RequestMethod.GET)
	@ApiOperation(value = "查询品牌信息", notes = "查询品牌信息(状态为正常)", produces = MediaType.APPLICATION_JSON_VALUE)
	@ResponseBody
	public ResultDTOWithPagination<Object> getBrands(PageVo pageVo, BrandQuery conditon) throws Exception {
		LOGGER.info("BrandSeriesRest.getBrands params : {}, {}", pageVo, conditon);
		ResultDTOWithPagination<Object> result = new ResultDTOWithPagination<>(true, null, "查询成功！", pageVo);
		try {
			BrandQueryConditions brandQueryConditions = new BrandQueryConditions();
			BeanUtils.copyProperties(conditon, brandQueryConditions);
			List<ScBaseCvBrand> list = scBaseCvBrandService.getAllList(pageVo, brandQueryConditions);
			result.setData(list);
		} catch (Exception e) {
			LOGGER.error("BrandSeriesRest.getBrands error: {}", e);
			result.setMessage(e.getMessage());
			result.setSuccess(false);
		}
		return result;
	}

	@RequestMapping(value = "/getSeries", method = RequestMethod.GET)
	@ApiOperation(value = "查询所有车系", notes = "查询所有车系(状态为正常)", produces = MediaType.APPLICATION_JSON_VALUE)
	@ResponseBody
	public ResultDTOWithPagination<Object> getSeries(PageVo pageVo, SeriesQuery conditon) throws Exception {
		LOGGER.info("BrandSeriesRest.getAllSeries params : {}, {}", pageVo, conditon);
		ResultDTOWithPagination<Object> result = new ResultDTOWithPagination<>(true, null, "查询成功！", pageVo);
		try {
			SeriesQueryBo seriesQueryBo = new SeriesQueryBo();
			BeanUtils.copyProperties(conditon, seriesQueryBo);
			List<ScBaseCvSeries> list = seriesService.getByCondition(pageVo, seriesQueryBo);
			result.setData(list);
		} catch (Exception e) {
			LOGGER.error("BrandSeriesRest.getSeries error: {}", e);
			result.setMessage(e.getMessage());
			result.setSuccess(false);
		}
		return result;
	}


	@RequestMapping(value = "/getBrandSeriesModel", method = RequestMethod.GET)
	@ApiOperation(value = "根据品牌查询对应的车系及车型", notes = "根据品牌查询对应的车系及车型(状态为正常)", produces = MediaType.APPLICATION_JSON_VALUE)
	@ResponseBody
	@ApiIgnore
	public ResultDTOWithPagination<Object> getBrandSeriesModel(PageVo pageVo, BrandQuery conditon) throws Exception {
		LOGGER.info("BrandSeriesRest.getBrandSeriesModel params : {}, {}", pageVo, conditon);
		ResultDTOWithPagination<Object> result = new ResultDTOWithPagination<>(true, null, "查询成功！", pageVo);
		try {
			ScBrandSeriesModelBo scBrandSeriesModelBo = new ScBrandSeriesModelBo();
			if (null != conditon) {
				scBrandSeriesModelBo.setBrandId(conditon.getId());
				scBrandSeriesModelBo.setBrandCode(conditon.getCode());
				scBrandSeriesModelBo.setBrandName(conditon.getName());
			}
			List<ScBaseCvBrandDto> list = scBaseCvBrandService.selectBrandSeriesModelByCondition(pageVo, scBrandSeriesModelBo);
			result.setData(list);
		} catch (Exception e) {
			LOGGER.error("BrandSeriesRest.getBrandSeriesModel error: {}", e);
			result.setMessage(e.getMessage());
			result.setSuccess(false);
		}
		return result;
	}


	@RequestMapping(value = "/getBrandsByPrefix/{prefix}", method = RequestMethod.GET)
	@ApiOperation(value = "根据品牌首字母查询品牌信息", notes = "根据首字母查询品牌信息，状态为正常", produces = MediaType.APPLICATION_JSON_VALUE)
	@ResponseBody
	public ResultDTOWithPagination<CvBrandDTO[]> getBrandByPrefix(@ApiParam(value = "brand prefix ", required = true)
																  @PathVariable("prefix") String prefix, PageVo pageVo) throws Exception {
		LOGGER.info("BrandSeriesRest.getBrandByPrefix params : {}", prefix, pageVo);
		ResultDTOWithPagination<CvBrandDTO[]> result = new ResultDTOWithPagination<>(true, null, "查询成功！");
		try {
			BrandQueryConditions conditions = new BrandQueryConditions();
			conditions.setPrefix(prefix);
			List<ScBaseCvBrand> list = scBaseCvBrandService.getAllList(pageVo, conditions);
			Integer count = scBaseCvBrandService.selectCountList(conditions);
			pageVo.setTotalRecord(count);
			CvBrandDTO[] listDTO = new CvBrandDTO[list.size()];
			for (int i = 0; i < list.size(); i++) {
				CvBrandDTO cvBrandDTO = new CvBrandDTO();
				BeanUtils.copyProperties(list.get(i), cvBrandDTO);
				listDTO[i] = cvBrandDTO;
			}
			result.setPageVo(pageVo);
			result.setData(listDTO);
		} catch (Exception e) {
			LOGGER.error("BrandSeriesRest.getBrandByPrefix error: {}", e);
			throw new BusinessException(e.getMessage());
		}
		return result;
	}


	/**
	 * Select CvBrand By name
	 *
	 * @param name
	 * @return
	 */

	@RequestMapping(value = "/get-brands-by-name/{name}", method = RequestMethod.GET)
	@ApiOperation(value = "根据品牌名称模糊查询品牌信息", notes = "根据品牌名称模糊查询品牌信息，状态为正常", produces = MediaType.APPLICATION_JSON_VALUE)
	@ResponseBody
	@ApiIgnore
	public ResultDTOWithPagination<CvBrandDTO[]> getBrandByName(@ApiParam(value = "brand name", required = true)
																@PathVariable("name") String name, PageVo pageVo) throws Exception {
		LOGGER.info("BrandSeriesRest.getBrandByName params : {}", name, pageVo);
		ResultDTOWithPagination<CvBrandDTO[]> result = new ResultDTOWithPagination<>(true, null, "查询成功");
		try {
			BrandQueryConditions conditions = new BrandQueryConditions();
			conditions.setName(name);
			List<ScBaseCvBrand> list = scBaseCvBrandService.getAllList(pageVo, conditions);
			Integer count = scBaseCvBrandService.selectCountList(conditions);
			pageVo.setTotalRecord(count);
			CvBrandDTO[] listDTO = new CvBrandDTO[list.size()];
			for (int i = 0; i < list.size(); i++) {
				CvBrandDTO cvBrandDTO = new CvBrandDTO();
				BeanUtils.copyProperties(list.get(i), cvBrandDTO);
				listDTO[i] = cvBrandDTO;
			}
			result.setPageVo(pageVo);
			result.setData(listDTO);
		} catch (Exception e) {
			LOGGER.error("BrandSeriesRest.getBrandByName error: {}", e);
			throw new BusinessException(e.getMessage());
		}
		return result;
	}


	/**
	 * select series by brandId
	 *
	 * @param brandId
	 * @return the result
	 */

	@RequestMapping(value = "/get-series-by-brandid/{brandid}", method = RequestMethod.GET)
	@ApiOperation(
			value = "根据品牌ID查询车系信息",
			notes = "根据品牌ID查询车系信息,状态为正常",
			produces = MediaType.APPLICATION_JSON_VALUE
	)
	@ResponseBody
	@ApiIgnore
	public ResultDTOWithPagination<CvSeriesDTO[]> getSeriesByBrandId(@ApiParam(value = "series brand_id", required = true)
																	 @PathVariable("brandid") Integer brandId, PageVo pageVo) throws Exception {
		LOGGER.info("BrandSeriesRest.getSeriesByBrandId params : {}", brandId, pageVo);
		ResultDTOWithPagination<CvSeriesDTO[]> result = new ResultDTOWithPagination<>(true, null, "查询成功");

		try {
			SeriesQueryConditions conditions = new SeriesQueryConditions();
			conditions.setBrandId(brandId);
			List<ScBaseCvSeries> list = seriesService.getAllList(pageVo, conditions);
			Integer count = seriesService.selectCountList(conditions);
			pageVo.setTotalRecord(count);
			CvSeriesDTO[] listDTO = new CvSeriesDTO[list.size()];
			for (int i = 0; i < list.size(); i++) {
				CvSeriesDTO cvSeriesDTO = new CvSeriesDTO();
				BeanUtils.copyProperties(list.get(i), cvSeriesDTO);
				listDTO[i] = cvSeriesDTO;
			}
			result.setPageVo(pageVo);
			result.setData(listDTO);
		} catch (Exception e) {
			LOGGER.error("BrandSeriesRest.getSeriesByBrandId error: {}", e);
			throw new BusinessException(e.getMessage());
		}
		return result;
	}

	@RequestMapping(value = "/getSeriesByName/{name}", method = RequestMethod.GET)
	@ApiOperation(
			value = "根据车系名称模糊查询车系信息",
			notes = "根据车系名称模糊查询车系信息,状态为正常",
			produces = MediaType.APPLICATION_JSON_VALUE
	)
	@ResponseBody
	@ApiIgnore
	public ResultDTOWithPagination<CvSeriesDTO[]> getSeriesByName(@ApiParam(value = "series name ", required = true)
																  @PathVariable("name") String name, PageVo pageVo) throws Exception {
		LOGGER.info("BrandSeriesRest.getSeriesByName params : {}", name, pageVo);
		ResultDTOWithPagination<CvSeriesDTO[]> result = new ResultDTOWithPagination<>(true, null, "查询成功");
		try {
			SeriesQueryConditions conditions = new SeriesQueryConditions();
			conditions.setName(name);
			List<ScBaseCvSeries> list = seriesService.getAllList(pageVo, conditions);
			Integer count = seriesService.selectCountList(conditions);
			pageVo.setTotalRecord(count);
			CvSeriesDTO[] listDTO = new CvSeriesDTO[list.size()];
			for (int i = 0; i < list.size(); i++) {
				CvSeriesDTO cvSeriesDTO = new CvSeriesDTO();
				BeanUtils.copyProperties(list.get(i), cvSeriesDTO);
				listDTO[i] = cvSeriesDTO;
			}
			result.setPageVo(pageVo);
			result.setData(listDTO);
		} catch (Exception e) {
			LOGGER.error("BrandSeriesRest.getSeriesByName error: {}", e);
			throw new BusinessException(e.getMessage());
		}
		return result;
	}

	@RequestMapping(value = "/queryBrands/{cvKeyword}", method = RequestMethod.GET)
	@ApiOperation(value = "根据输入参数，查询所有满足条件的商品车型", notes = "根据输入参数，查询所有满足条件的商品车型)", produces = MediaType.APPLICATION_JSON_VALUE)
	@ResponseBody
	public ResultDTOWithPagination<ModelDTO[]> getByCvKeyword(@ApiParam(value = "cvKeyword", required = true)
															  @PathVariable("cvKeyword") String cvKeyword, PageVo pageVo) throws Exception {
		LOGGER.info("BrandSeriesRest.getByCvKeyword params : {}", cvKeyword, pageVo);
		ResultDTOWithPagination<ModelDTO[]> result = new ResultDTOWithPagination<>(true, null, "查询成功");
		try {
			pageVo.setPageSize(100);
			List<BrandSeriesModel> list = scBaseCvBrandService.getListByParam(pageVo, cvKeyword);
			Integer count = scBaseCvBrandService.selectCountListByParam(cvKeyword);
			pageVo.setTotalRecord(count);
			ModelDTO[] listDTO = new ModelDTO[list.size()];
			for (int i = 0; i < list.size(); i++) {
				ModelDTO modelDTO = new ModelDTO();
				BeanUtils.copyProperties(list.get(i), modelDTO);
//                根据key去七牛获取url返回  sales系统处理
//                String qiniuKey = list.get(i).getBrandLogoUrl();
//                String logoUrl = QiniuUtil.generateDownloadURL(QiniuConstant.QINIU_LOGO_DOWNLOAD_ADDRESS, qiniuKey, "", null, null);
//                modelDTO.setBrandLogoUrl(logoUrl);
				listDTO[i] = modelDTO;
			}
			result.setPageVo(pageVo);
			result.setData(listDTO);
		} catch (Exception e) {
			LOGGER.error("BrandSeriesRest.getByCvKeyword error: {}", e);
			throw new BusinessException(e.getMessage());
		}
		return result;
	}
}
